<template>
    <div>
        <el-card class="box-card">
            <el-button type="primary" size="default" @click="addTrademark" icon="Plus">添加品牌</el-button>
            <el-table style="margin: 10px 0 ;" border :data="trademarkArr">
                <el-table-column label="序号" width="80px" align="center" type="index"></el-table-column>
                <el-table-column label="品牌名称">
                    <template #="{ row }">
                        {{ row.tmName }}
                    </template>
                </el-table-column>
                <el-table-column label="品牌logo">
                    <template #="{ row }">
                        <img :src="row.logoUrl" style="width: 100px; height: 100px;">
                    </template>
                </el-table-column>
                <el-table-column label="品牌操作">
                    <template #="{ row }">
                        <el-button type="primary" size="small" icon="Edit" @click="updateTrademark(row)"></el-button>
                        <el-popconfirm
                            width="250px"
                            icon="Delete"
                            @confirm="removeTrademark(row.id)"
                            :title="`确定要删除${row.tmName}吗?`">
                            <template #reference>
                                <el-button type="primary" size="small" icon="Delete"></el-button>
                            </template>
                        </el-popconfirm>
                        
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="sizeChange" @current-change="getHasTrademark" v-model:current-page="pageNo"
                v-model:page-size="limit" :page-sizes="[3, 5, 7, 9]" :background="true"
                layout="prev, pager, next, jumper,->,total, sizes" :total="total" />

        </el-card>
        <el-dialog v-model="dialogFormVisible" :title="trademarkParams.id?'修改品牌':'添加品牌'">
            <el-form style="width: 80%;" :model="trademarkParams" :rules="rules" ref="formRef">
                <el-form-item label="品牌名称" label-width="100px" prop="tmName">
                    <el-input v-model="trademarkParams.tmName" placeholder="请您输入品牌名称" ></el-input>
                </el-form-item>
                <el-form-item label="品牌LOGO" label-width="100px" prop="logoUrl">
                    <el-upload class="avatar-uploader"
                        action="http://114.115.179.162:8022/prod-api/admin/product/fileUpload" :show-file-list="false"
                        :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="trademarkParams.logoUrl" :src="trademarkParams.logoUrl" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button type="primary" size="default" @click="cancel">取消</el-button>
                <el-button type="primary" size="default" @click="confrim">确定</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive, nextTick } from 'vue'
import { TradeMark } from '@/api/product/trademark/type';
import { reqHasTrademark, reqAddOrUpdateTrademark,reqDeleteTrademark } from '@/api/product/trademark/index'
import { Records, TradeMarkResponseData } from '@/api/product/trademark/type';
import { ElMessage, type UploadProps } from 'element-plus'
let pageNo = ref<number>(1)
let limit = ref<number>(3)
let total = ref<number>(0)
let trademarkArr = ref<Records>([])
let dialogFormVisible = ref<boolean>(false)
let trademarkParams = reactive<TradeMark>({
    tmName: "",
    logoUrl: ''
})
let formRef = ref()
const addTrademark = () => {
    dialogFormVisible.value = true
    trademarkParams.logoUrl = ''
    trademarkParams.tmName = ''
    trademarkParams.id = 0
    nextTick(()=>{
        formRef.value.clearValidate('tmName')
        formRef.value.clearValidate('logoUrl')
    })
    
}

const updateTrademark = (row: any) => {
    nextTick(()=>{
        formRef.value.clearValidate('tmName')
        formRef.value.clearValidate('logoUrl')
    })
    Object.assign(trademarkParams,row)
    dialogFormVisible.value = true
}

const getHasTrademark = async (page=1) => {
    pageNo.value=page
    let result: TradeMarkResponseData = await reqHasTrademark(pageNo.value, limit.value)
    if (result.code == 200) {
        total.value = result.data.total
        trademarkArr.value = result.data.records
    }

}
const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    trademarkParams.logoUrl = response.data
    formRef.value.clearValidate('logoUrl')
}
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type == 'image/png' || rawFile.type == 'image/jpeg' || rawFile.type == 'image/gif') {
        if (rawFile.size / 1024 / 1024 < 4) {
            return true
        } else {
            ElMessage({
                type: 'error',
                message: '上传文件大小必须小于4M'
            })
            return false
        }
    } else {
        ElMessage({
            type: 'error',
            message: '上传文件格式必须为png|jpg|gif'
        })
        return false
    }
}
const sizeChange = () => {
    pageNo.value = 1
    getHasTrademark()
}

const cancel = () => {
    dialogFormVisible.value = false
}

const confrim = async () => {
    await formRef.value.validate()
    let result: any = await reqAddOrUpdateTrademark(trademarkParams)
    if (result.code == 200) {
        dialogFormVisible.value = false
        ElMessage({
            type: 'success',
            message: trademarkParams.id?'修改品牌成功':'添加品牌成功'
        })
        getHasTrademark(trademarkParams.id?pageNo.value:1)
    } else {
        ElMessage({
            type: 'error',
            message: trademarkParams.id?'修改品牌失败':'添加品牌失败'
        })
        dialogFormVisible.value = false
    }

}

onMounted(() => {
    getHasTrademark()
})
const validatorTmName = (rule:any,value:any,callBack:any)=>{
    if(value.trim().length>=2){
        callBack()
    }else{
        callBack(new Error('品牌名称位数大于等于两位'))
    }
}
const validatorLogoUrl = (rule:any,value:any,callBack:any)=>{
    if(value){
        callBack()
    }else{
        callBack(new Error('logo图片务必上传'))
    }
    
}
const rules = {
    tmName:[
        {
            required:true,
            trigger:'blur',
            validator:validatorTmName
        }
    ],
    logoUrl:[
        {
            required:true,
            validator:validatorLogoUrl
        }
    ]
}
const removeTrademark =async(id:number)=>{
   let result =  await reqDeleteTrademark(id)
   if(result.code==200){
        ElMessage({
            type:'success',
            message:'删除成功'
        })
        getHasTrademark(trademarkArr.value.length>1?pageNo.value:pageNo.value-1)
    }else{
        ElMessage({
            type:'error',
            message:'删除失败'
        })
    }
}
</script>

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>